<!DOCTYPE html>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!--360浏览器优先以webkit内核解析-->
    <title>睡眠中心</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
    <link href="../static/css/font-awesome.min.css" th:href="@{/css/font-awesome.min.css}" rel="stylesheet"/>
    <link href="../static/css/style.min.css" th:href="@{/css/style.min.css}" rel="stylesheet"/>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="row">
        <h2 align="center">睡眠中心管理</h2>
        <div class="panel-body">
            <div class="col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4>正常</h4>
                    </div>
                    <div class="panel-body">
                        <div class="col-sm-12 select-table table-striped">
                            <table id="bootstrap-table1"></table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4>异常</h4>
                    </div>
                    <div class="panel-body">
                        <div class="col-sm-12 select-table table-striped">
                            <table id="bootstrap-table2"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/bootstrap.min.js}"></script>
<script th:src="@{/ajax/libs/layer/layer.min.js}"></script>
<script th:src="@{/js/sockjs.min.js}"></script>
<script th:src="@{/js/stomp.js}"></script>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "common";
    var userId = [[${userId}]];
    var datas1 = [[${@dict.getType('someone_exists_type')}]];
    var datas2 = [[${@dict.getType('device_online_type')}]];
    var data1 = [];
    var data2 = [];
    $.ajax({
        type: "post",
        dataType: "json",
        data: {
            userId: userId
        },
        url: "/common/queryRadarByUser",
        success: function (res) {
            if(res.code != 0){
                $.modal.msgError(res.msg);
            }else{
                // data = res.data;
                var data = res.data;
                data.forEach(function(item,index,self){
                    if(item.online == 1){
                        if('heartRateValue' in item){
                            if(item.heartRateValue >= 61 && item.heartRateValue <= 99){
                                data1.push(item);
                            }{
                                Object.assign(item, {"omenType": "心率异常"});
                                data2.push(item);
                            }
                        }else{
                            data1.push(item);
                        }
                    }else{
                        Object.assign(item, {"omenType": "设备离线"});
                        data2.push(item);
                    }
                });
                for (let i = data1.length - 1; i >= 0; i--) {
                    for (let j = 0; j < data2.length; j++) {
                        if(data1[i].deviceCode == data2[j].deviceCode){
                            data1.slice(i,1);
                        }
                    }
                }
                $.table.init(getOption("bootstrap-table1",data1));
                $.table.init(getErrorOption("bootstrap-table2",data2));

            }
        }
    });
    var stomp = null;
    var url = "https://sunfu.xdkdate.com/webSocket";
    // var url = "https://www.bythealthy.com/sunfu/webSocket"
    // var url = "http://localhost:8009/webSocket"
    var socket = new SockJS(url);
    stomp = Stomp.over(socket);
    //连接
    stomp.connect({}, function (frame) {
        //订阅广播
        stomp.subscribe("/user/radars/message", function (res) {
            var obj = eval('('+res.body+')');
            data1.forEach(function(item,index,self){
                if(item.deviceCode == obj.deviceCode) {
                    var key = obj.key;
                    var value = obj.value;
                    if(key == "heartRateValue") {
                        Object.assign(item, {"heartRateValue": value});
                    }
                    if(key == "breathValue") {
                        Object.assign(item, {"breathValue": value});
                    }
                    if(key == "sleepStatus") {
                        Object.assign(item, {"sleepStatus": value});
                    }
                    if(key == "motionStatus") {
                        Object.assign(item, {"motionStatus": value});
                    }
                    if(key == "someoneExists") {
                        Object.assign(item, {"someoneExists": value});
                    }
                    if(key == "online") {
                        Object.assign(item, {"online": value});
                    }
                    if(key == "fallStatus") {
                        if(value == "跌倒"){
                            Object.assign(item, {"fallStatus": value});
                            Object.assign(item, {"omenType": "用户跌倒"});
                            data2.push(item);
                        }
                    }
                }
            });
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    userId: userId,
                    omenListStr: JSON.stringify(data2)
                },
                url: "/common/addOmen",
                success: function (res) {
                    if(res.code != 0){
                        $.modal.msgError(res.msg);
                    }else{
                    }
                }
            });
            data2.forEach(function(item,index,self){
                if(item.deviceCode == obj.deviceCode) {
                    var key = obj.key;
                    var value = obj.value;
                    if(key == "heartRateValue") {
                        Object.assign(item, {"heartRateValue": value});
                    }
                    if(key == "breathValue") {
                        Object.assign(item, {"breathValue": value});
                    }
                    if(key == "fallStatus") {
                        Object.assign(item, {"fallStatus": value});
                    }
                    if(key == "motionStatus") {
                        Object.assign(item, {"motionStatus": value});
                    }
                    if(key == "online") {
                        Object.assign(item, {"online": value});
                    }
                }
            });
            for (let i = data1.length - 1; i >= 0; i--) {
                for (let j = 0; j < data2.length; j++) {
                    if(data1[i].deviceCode == data2[j].deviceCode){
                        data1.slice(i,1);
                    }
                }
            }
            $.table.refreshOptions(getOption("bootstrap-table1",data1),"bootstrap-table1");
            $.table.refreshOptions(getErrorOption("bootstrap-table2",data2),"bootstrap-table2");
        });
    });
    function getOption(tableId,data){
        var options = {
            data: data,
            id: tableId,
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            columns: [{
                    field : 'roomNum',
                    title : '房间号'
                },{
                    field : 'roomName',
                    title : '房间名称'
                },
                {
                    field : 'username',
                    title : '用户姓名'
                },
                {
                    field : 'heartRateValue',
                    title : '心率'
                },
                {
                    field : 'breathValue',
                    title : '呼吸率'
                },
                {
                    field : 'sleepStatus',
                    title : '睡眠状态'
                },
                {
                    field : 'motionStatus',
                    title : '运动状态'
                },
                {
                    field : 'someoneExists',
                    title : '人体状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(datas1, value);
                    }
                },
                {
                    field : 'online',
                    title : '在线状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(datas2, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="#" onclick="toRoomReport(\'' + row.userId + '\',\'' + row.deviceCode + '\')"><i class="fa fa-search"></i>详情</a> ');
                        return actions.join('');
                    }
                }]
        };
        return options;
    }
    function getErrorOption(tableId,data){
        var options = {
            data: data,
            id: tableId,
            pagination: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            columns: [{
                    field : 'roomNum',
                    title : '房间号'
                },{
                    field : 'roomName',
                    title : '房间名称'
                },
                {
                    field : 'username',
                    title : '用户姓名'
                },
                {
                    field : 'heartRateValue',
                    title : '心率'
                },
                {
                    field : 'breathValue',
                    title : '呼吸率'
                },
                {
                    field : 'fallStatus',
                    title : '摔倒状态'
                },
                {
                    field : 'motionStatus',
                    title : '运动状态'
                },
                {
                    field : 'healthyStatus',
                    title : '健康状态'
                },
                {
                    field : 'online',
                    title : '在线状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(datas2, value);
                    }
                },
                {
                    field : 'omenType',
                    title : '告警类型'
                },
                {
                    field : 'omenLamp',
                    title : '告警指示灯',
                    formatter: function(value, row, index) {
                        return $.table.imageView('https://sunfu.xdkdate.com/profile/004.gif');
                        // return $.table.imageView('https://www.bythealthy.com/sunfu/profile/004.gif');
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="#" onclick="handleError(\'' + row.userId + '\')"><i class="fa fa-edit"></i>处理</a> ');
                        return actions.join('');
                    }
                }]
        };
        return options;
    }
    function toRoomReport(userId,deviceCode){
        if(userId=="undefined" || userId=="" || userId==null) {
            $.modal.msgError("当前设备没绑定用户");
        }else{
            // $(location).attr('href', 'https://sunfu.xdkdate.com/common/roomReport?userId=' + userId + '&deviceCode=' + deviceCode);

            $.modal.openTab('睡眠报告', prefix + '/roomReport?userId=' + userId + '&deviceCode=' + deviceCode);
        }
    }
    function handleError(userId){
        $.modal.open('用户信息', prefix + '/queryUser?userId=' + userId);
        // $.ajax({
        //     type: "post",
        //     dataType: "json",
        //     data: {
        //         userId: userId,
        //         omenId: omenId
        //     },
        //     url: "/common/handleOmen",
        //     success: function (res) {
        //         if(res.code != 0){
        //             $.modal.msgError(res.msg);
        //         }else{
        //             $.modal.msgSuccess(res.msg);
        //             // data2 = res.data;
        //             // $.table.refreshOptions(getErrorOption("bootstrap-table2",data2),"bootstrap-table2");
        //         }
        //     }
        // });
    }
</script>
</body>
</html>
